// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const MAX_LIMIT = 100
const userinfo = db.collection('userinfo')
const userpay = db.collection('userpay')

// 云函数入口函数

exports.main = async (event, context) => {
  let templist = []
  let totalPri = 0
  let less = []
  let more = []
  let resault = []
  let average = 0
  const num = await userinfo.where({ itemName: event.name }).count()
  const userlist = await userinfo.where({ itemName: event.name }).get()
  for (let key in userlist.data){
    let tempresault = {
      user: {},
      totalprice: 0
    }
    let temp = 0
    let templist1 = []
    let countResult = await db.collection('userpay').where({ _openid: userlist.data[key]._openid,itemName:event.name }).count()
    let total = countResult.total
    // 计算需分几次取
    let batchTimes = Math.ceil(total / 100)
    // 承载所有读操作的 promise 的数组
    for (let i = 0; i < batchTimes; i++) {
      let promise = await db.collection('userpay').where({ _openid: userlist.data[key]._openid, itemName: event.name }).skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
      templist1.push.apply(templist1, promise.data)
    }
    for(let key1 in templist1){
      temp = temp + templist1[key1].price
    }
    totalPri = totalPri + temp
    tempresault.user = userlist.data[key].user
    tempresault.totalprice = temp
    templist[key] = tempresault
  }
  average = (totalPri / num.total)
  average = average.toFixed(2)
  for(let each in templist){
    let detail = {}
    if(templist[each].totalprice <= average){
      detail.user = templist[each].user
      detail.pay = (average * 100 - templist[each].totalprice * 100) / 100
      less.push(detail)
    }
    else{
      detail.user = templist[each].user
      detail.pay = (templist[each].totalprice * 100 - average * 100) / 100
      more.push(detail)
    }
  }
  console.log(less)
  console.log(more)
  let len = less.length
  for(let less_each = 0; less_each < len; less_each++){
    if(less[less_each].pay == 0){
      continue
    }
    for(let more_each = 0; more_each < more.length; more_each++) {
      if(more[more_each].pay == 0){
        continue
      }
      let temp = {}
      if(less[less_each].pay <= more[more_each].pay) {
        temp.payfrom = less[less_each].user
        temp.payto = more[more_each].user
        temp.pay = less[less_each].pay
        more[more_each].pay = (more[more_each].pay - less[less_each].pay).toFixed(2)
        less[less_each].pay = 0
        resault.push(temp)
        console.log(more[more_each])
        console.log(temp)
        break
      }
      else{
        temp.payfrom = less[less_each].user
        temp.payto = more[more_each].user
        temp.pay = more[more_each].pay
        less[less_each].pay = (less[less_each].pay - more[more_each].pay).toFixed(2)
        more[more_each].pay = 0
        resault.push(temp)
        console.log(temp)
      }
    }
  }
  return {
    templist,
    resault,
    totalPri,
    average,
    num
  }

  // let templist = []
  // let totalPri = 0
  // let less = []
  // let more = []
  // let resault = []
  // let average = 0
  // const num = await userinfo.where({ itemName: event.name }).count()
  // const userlist = await userinfo.where({ itemName: event.name }).get()
  // for (let key in userlist.data) {
  //   let tempresault = {
  //     user: {},
  //     totalprice: 0
  //   }
  //   let temp = 0
  //   let templist1 = []
  //   templist1 = await userpay.where({ _openid: userlist.data[key]._openid }).get()
  //   for (let key1 in templist1.data) {
  //     temp = temp + templist1.data[key1].price
  //   }
  //   totalPri = totalPri + temp
  //   tempresault.user = userlist.data[key].user
  //   tempresault.totalprice = temp
  //   templist[key] = tempresault
  // }
  // console.log(num.total)
  // average = (totalPri / num.total)
  // average = average.toFixed(2)
  // for (let each in templist) {
  //   let detail = {}
  //   if (templist[each].totalprice <= average) {
  //     detail.user = templist[each].user
  //     detail.pay = average - templist[each].totalprice
  //     less.push(detail)
  //   }
  //   else {
  //     detail.user = templist[each].user
  //     detail.pay = templist[each].totalprice - average
  //     more.push(detail)
  //   }
  // }
  // let len = less.length
  // for (let less_each = 0; less_each < len; less_each++) {
  //   if (less[less_each].pay == 0) {
  //     continue
  //   }
  //   for (let more_each = 0; more_each < more.length; more_each++) {
  //     if (more[more_each].pay == 0) {
  //       continue
  //     }
  //     let temp = {}
  //     if (less[less_each].pay <= more[more_each].pay) {
  //       temp.payfrom = less[less_each].user
  //       temp.payto = more[more_each].user
  //       temp.pay = less[less_each].pay
  //       less[less_each].pay = 0
  //       more[more_each].pay = more[more_each].pay - less[less_each].pay
  //       resault.push(temp)
  //       break
  //     }
  //     else {
  //       temp.payfrom = less[less_each].user
  //       temp.payto = more[more_each].user
  //       temp.pay = more[more_each].pay
  //       less[less_each].pay = less[less_each].pay - more[more_each].pay
  //       more[more_each].pay = 0
  //       resault.push(temp)
  //     }
  //   }
  // }
  // return {
  //   templist,
  //   resault,
  //   totalPri,
  //   average,
  //   num
  // }
}